package com.bds.btcdc.adapter.okcoin;

import com.bds.btcdc.adapter.zb.service.ZBTradeService;
import com.bds.btcdc.http.MD5Util;
import com.bds.btcdc.util.HttpUtilManager;
import org.apache.http.HttpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Created by DongChenchen on 2018/5/9 0008
 */
public class OkCoinAPIClient {
    private static final Logger logger = LoggerFactory.getLogger(ZBTradeService.class);
    private String accessKey;
    private String secretKey;
    private final String USERINFO_URL = "/api/v1/userinfo.do?";//获取用户信息URL
    private final String URL_PREX = "https://www.okcoin.com";
    private static ConcurrentMap<Long, OkCoinAPIClient> userAccess=new ConcurrentHashMap<>();
    private static OkCoinAPIClient client = null;
    public static OkCoinAPIClient getInstance(Long user_id, String accessKey, String secretKey) {
        if(!userAccess.containsKey(user_id)) {
            client=new OkCoinAPIClient(accessKey,secretKey);
            userAccess.put(user_id,client);
            return client;
        }else{
            return userAccess.get(user_id);
        }
    }
    private OkCoinAPIClient() {
    }
    private OkCoinAPIClient(String accessKey, String secretKey) {
        this.accessKey=accessKey;
        this.secretKey=secretKey;
    }

    /**
     * 获取用户信息
     * 访问频率 6次/2秒
     * @return
     * # Response
            {
                "info": {
                    "funds": {
                        "asset": { //账户资产，包含净资产及总资产
                            "net": "0", //净资产(不包含借款)
                            "total": "0" //总资产(包含借款)
                        },
                        "borrow": { //账户借款信息(只有在账户有借款信息时才会返回)
                            "btc": "0",
                            "usd": "0",
                            "ltc": "0"
                        },
                        "free": { //账户余额
                            "btc": "0",
                            "usd": "0",
                            "ltc": "0",
                            "eth": "0"
                        },
                        "freezed": {//账户冻结余额
                            "btc": "0",
                            "usd": "0",
                            "ltc": "0",
                            "eth": "0"
                        },
                        "union_fund": {//账户理财信息(只有在账户有理财信息时才返回)
                            "btc": "0",
                            "ltc": "0"
                        }
                    }
                },
                "result": true
            }
     */
    public String getAccountInfo() throws HttpException, IOException {
            // 构造参数签名
            Map<String, String> params = new HashMap<String, String>();
            params.put("api_key", accessKey);
            String sign = MD5Util.buildMysignV1(params, this.secretKey);
            params.put("sign", sign);
            // 发送post请求
            HttpUtilManager httpUtil = HttpUtilManager.getInstance();
        return httpUtil.requestHttpPost(URL_PREX,this.USERINFO_URL,params);
    }
}
